import echarts from 'echarts';
import config from './config';
import CityPos from './citys';

// 图形数据加载器
let DataLoader;
DataLoader = {
  'singleSeries': function (option, data) {
    let x = data.x;
    // let y = data.y[0];
    // map((d) => { x.push(d.name); return d.value; });
    let xAxis = $.isArray(option.xAxis) ? option.xAxis[0] : option.xAxis;
    let ser = $.isArray(option.series) ? option.series[0] : option.series;
    option.series = data.y.map((d) => {
      let s = {'name': d.name};
      // debugger
      $.extend(true, s, ser);
      s['data'] = d.value;

      if (d.hidden) {
        s['itemStyle'] = { opacity:0 };
        s['lineStyle'] = { opacity:0 };
      }
      return s;
    });
    xAxis['data'] = x;
    console.log(option)
    // ser['data'] = y;
  },
  'linebar': function (option, data) {
    let x = data.x;
    let xAxis = $.isArray(option.xAxis) ? option.xAxis[0] : option.xAxis;
    let keys = Object.keys(data).filter((k) => { return k !== 'x'; }).reduce((a, b) => {
      a[b] = {};
      return a;
    }, {});
    option.series.forEach((s) => {
      if (s.type in keys) keys[s.type] = s;
    });
    option.series = [];
    for (let key in keys) {
      data[key].forEach((d) => {
        let s = {};
        $.extend(true, s, keys[key]);
        s['name'] = d.name;
        s['data'] = d.value;
        option.series.push(s);
      });
    }

    xAxis['data'] = x;
  },
  'mapLines': function (option, graph) {
    let keys = {'lines': {}, 'effectScatter': {}, 'scatter': {}};
    option.series.forEach((s) => {
      if (s.type in keys) keys[s.type] = s;
    });
    let nodes = graph.nodes;
    keys['lines']['data'] = graph.links.map((ln) => {
      let coords = [CityPos[nodes[ln.source].name], CityPos[nodes[ln.target].name]];
      return {fromName: nodes[ln.source].name, toName: nodes[ln.target].name, coords: coords};
    });
    keys['effectScatter']['data'] = nodes.map((d) => CityPos[d.name].concat(d.value));
  },
  'barHorizontal': function (option, data) {
    let y = [];
    let x = data.map((d) => { y.push(d.name); return d.value; });
    let yAxis = $.isArray(option.yAxis) ? option.yAxis[0] : option.yAxis;
    let ser = $.isArray(option.series) ? option.series[0] : option.series;
    yAxis['data'] = y;
    ser['data'] = x;
  },
  'common': function (option, data) {
    let x = [];
    let y = data.map((d) => { x.push(d.name); return d.value; });
    let xAxis = $.isArray(option.xAxis) ? option.xAxis[0] : option.xAxis;
    let ser = $.isArray(option.series) ? option.series[0] : option.series;
    xAxis['data'] = x;
    ser['data'] = y;
  },
  'pie': function (option, data) {
    let ser = $.isArray(option.series) ? option.series[0] : option.series;
    option.legend.data = data.map((d) => d.name);
    ser['data'] = data;
  },
  'com': function (option, data) {
    let ser = $.isArray(option.series) ? option.series[0] : option.series;
    // option.legend.data = data.map((d) => d.name);
    ser['data'] = data;
  },
  'pieTwo': function (option, data) {
    let legend = $.isArray(option.legend) ? option.legend[0] : option.legend;
    let colorP = option.color;
    let parents = data.map(d => ({name: d.name, value: d.value.reduce((a, b) => (a + b.value), 0)}));
    let children = data.reduce((a, b, i) => a.concat(b.value.map(e => {
      e['itemStyle'] = {color: colorP[i]};
      return e;
    })), []);
    legend['data'] = parents.map(d => d.name);
    option.series[0]['data'] = parents;
    option.series[1]['data'] = children;
  },
  'local1': function (option, graph) {
    let legend = $.isArray(option.legend) ? option.legend[0] : option.legend;
    let groups = graph.nodes.reduce((a, b) => {
      if (!(b.category in a)) a[b.category] = 1;
      return a;
    }, {});
    let categories = graph['categories'] || Object.keys(groups).map((d) => ({'name': '类' + d}));
    let ser = $.isArray(option.series) ? option.series[0] : option.series;
    legend['data'] = categories;
    ser['data'] = graph.nodes;
    ser['links'] = graph.links;
    ser['categories'] = categories;
  }
};

export default function DataLoad (option, name, data, dataLoader) {
  const common = {'bar': 'common', 'line': 'common', 'worldMap': 'com', 'pie': 'pie'};
  let tp = dataLoader || (name in common ? common[name] : name);
  if (!(tp in DataLoader)) return;
  DataLoader[tp](option, data);
}

// export default Chart;

/*

let graph = {
  nodes: [{name: '成都', category: 0}, {name: '上海', category: 1}, {name: '北京', category: 1},
    {name: '深圳', category: 1}, {name: '广州', category: 1}],
  links: [{source: 0, target: 1}, {source: 1, target: 2}, {source: 2, target: 0}, {source: 0, target: 3},
    {source: 0, target: 4}, {source: 0, target: 5}
  ]
};
let data = [
  {name: '先进制造业', value: [{name: '产业1', value: 20}, {name: '产业2', value: 20}, {name: '产业3', value: 20}]},
  {name: '二大业', value: [{name: '产业5', value: 10}, {name: '产业6', value: 10}, {name: '产业7', value: 10}]},
  {name: '都市现代业', value: [{name: '产业9', value: 10}, {name: '产业10', value: 10}, {name: '产业11', value: 10}]}
];
* */
